package edu.iscas.appcheckclient.fragment;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.iscas.appcheckclient.R;
import edu.iscas.appcheckclient.model.actions.Action;
import edu.iscas.appcheckclient.service.RecordService;

/**
 * Created by dell on 2016/1/12.
 */
public class HttpResponseFragment extends android.support.v4.app.Fragment implements ListAdapter {

    Set<DataSetObserver> observerSet = new HashSet<>();
    protected List<Action> mockedResponseList;
    protected ListView mockedResponseListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mockedResponseList = RecordService.getMockedHttpResponseList();
        View rootView = inflater.inflate(R.layout.fragment_http_response_list, container, false);
        mockedResponseListView = (ListView)rootView.findViewById(R.id.lv_http_response);
        this.mockedResponseListView.setAdapter(this);
        return rootView;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        if((position < 0) || (position >= this.mockedResponseList.size())){
            throw new ArrayIndexOutOfBoundsException(position);
        }
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        observerSet.add(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        observerSet.remove(observer);
    }

    @Override
    public int getCount() {
        return this.mockedResponseList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.mockedResponseList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if((convertView != null) && (convertView instanceof LinearLayout)){
            ((LinearLayout) convertView).removeAllViews();
        }else{
            convertView = new LinearLayout(parent.getContext());
            ((LinearLayout)convertView).setOrientation(LinearLayout.VERTICAL);
        }
        Action action = this.mockedResponseList.get(position);
        TextView typeTextView = new TextView(parent.getContext());
        typeTextView.setText(action.getTypeString());
        ((LinearLayout)convertView).addView(typeTextView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        TextView valueTextView = new TextView(parent.getContext());
        valueTextView.setText(action.getValue());
        ((LinearLayout)convertView).addView(valueTextView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }
}
